<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare(strict_types=1);

namespace app\unify\middleware;

use Closure;
use think\Config;
use think\Log;
use think\Request;
use think\Response;

/**
 * 跨域请求支持
 */
class AllowCrossDomain
{
    /**
     * 设置跨域
     * @param $request
     * @param \Closure $next
     * @return mixed|void
     */
    public function handle(Request $request, \Closure $next)
    {

        // 允许所有来源的跨域请求
        header('Access-Control-Allow-Origin: *');
        // 允许的请求方法
        header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
        // 允许的请求头
        header('Access-Control-Allow-Headers: *');
        // 暴露其他响应头给客户端
        header('Access-Control-Expose-Headers: Content-Length,Content-Range,Content-Type');
        // 处理预检请求的配置
        if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
//            header('Access-Control-Max-Age: 1728000');
//            header('Content-Type: text/plain; charset=utf-8');
//            header('Content-Length: 0');
//            http_response_code(204);
//            exit;
            exit();
        }


        return $next($request);
    }
}
